const BaseController = require('../BaseController');

class UserController extends BaseController {
	// 登录
	async login() {
		const { ctx, app, service } = this;
		const body = ctx.request.body;
		const res = await service.admin.userService.login(body);
		// 查询数据库，看用户是否存在
		this.ok(res);
	}
	// 查询所有用户
	async queryUser() {
		const { ctx, app, service } = this;
		const body = ctx.request.body;
		const res = await service.admin.userService.queryUser(body);
		this.ok(res);
	}
	// 创建用户
	async createUser() {
		const { ctx, app, service } = this;
		const body = ctx.request.body;
		ctx.validate({
			username: { type: 'string', required: true, message: '用户名必须存在' }
		});
		console.log('======body======', body);
		const res = await service.admin.userService.createUser(body);
		this.ok(res);
	}
	// 更新用户不做，创建既不能修改

	// 删除用户
	async deleteUser() {
		const { ctx, app, service } = this;
		const id = ctx.params.id;
		// 默认会对body校验
        ctx.validate({
			id: { type: 'string', required: true }
		}, { id });
		const res = await service.admin.userService.deleteUser(id);
		this.ok(res);
	}
}

module.exports = UserController;
